package com.fyzk.data.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDBHelper extends SQLiteOpenHelper{
	
	private static final String DATABASE_NAME = "DAJIANG.db";
	private static final int DATABASE_VERSION = 1;
	
	//表 —— 历史开奖
	/**
	 * 表名
	 */
	private static final String TABLE_NAME_KJ = "kaijiang_table";
	/**
	 * 表id
	 */
	public static final String KJ_ID = "kj_id";
	/**
	 * 本期销量
	 */
	private static final String KJ_XL = "totalmoney";
	/**
	 * 奖池滚存
	 */
	private static final String KJ_GC = "ccmoney";
	/**
	 * 三,四,五,六等奖的中奖彩金,注数
	 */
	private static final String KJ_ZJ3456 = "more3";
	/**
	 * 彩种
	 */
	private static final String KJ_CZ = "gamecode";
	/**
	 * 期号
	 */
	private static final String KJ_QH = "issue";
	/**
	 * 开奖号
	 */
	private static final String KJ_KJH = "bonusnum";
	/**
	 * 一等奖单注奖金
	 */
	private static final String KJ_ZJ1 = "money1";
	/**
	 * 二等奖单注奖金
	 */
	private static final String KJ_ZJ2 = "money2";
	/**
	 * id
	 */
	private static final String KJ_KJID = "id";
	/**
	 * 兑奖截止日期
	 */
	private static final String KJ_DJJZ = "encashtime";
	/**
	 * 二等奖中奖注数
	 */
	private static final String KJ_ZJZS2 = "number2";
	/**
	 * 一等奖中奖注数
	 */
	private static final String KJ_ZJZS1 = "number1";
	/**
	 * 开奖日期
	 */
	private static final String KJ_KJRQ	= "resulttime";

	public MyDBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

		String sql = "CREATE TABLE " + TABLE_NAME_KJ +"(" + KJ_ID + " INTEGER primary key autoincrement," + 
				KJ_XL +" text," + KJ_GC + " text," + KJ_ZJ3456 + " text," + KJ_CZ + " text," 
				 + KJ_QH + " text," + KJ_KJH + " text," + KJ_ZJ1 + " text," + KJ_ZJ2 + " text,"
				 + KJ_KJID + " INTEGER," + KJ_DJJZ + " text," + KJ_ZJZS2 + " text," + KJ_ZJZS1 + " text,"
				 + KJ_KJRQ + " text);";
					
		db.execSQL(sql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		String sql = "DROP TABLE IF EXISTS "+TABLE_NAME_KJ;
		db.execSQL(sql);
		onCreate(db);
		
	}
	
	/**
	 * 查询
	 * @param table_name	表名
	 * @param where			按什么排序
	 * @param limit 		查询行数  例如："2,5" 表示从第二行开始查，向下查询5条记录（0为起始行）
	 * @return
	 */
	public Cursor select(String table_name,String where,String limit){
		SQLiteDatabase db = this.getWritableDatabase();
		Cursor cursor = db.query(table_name, null, null, null, null, null, where,limit);
		return cursor;
	}
	
	/**
	 * 增加（插入）
	 * @param table_name	表名
	 * @param values		数据集
	 * @return
	 */
	public long insert(String table_name,ContentValues values){
		SQLiteDatabase db = this.getWritableDatabase();
		return db.insert(table_name, null, values);
	}

	/**
	 * 删除
	 * @param table_name	表名
	 * @param key			字段名
	 * @param values		删除条件
	 */
	public void delet(String table_name,String key,String[] values){
		SQLiteDatabase db = this.getWritableDatabase();
		db.delete(table_name, key+"=?", values);
	}
	
	/**
	 * 修改
	 * @param table_name	表名
	 * @param values		修改内容
	 * @param key			字段名
	 * @param valuesStrings	修改条件
	 */
	public void update(String table_name,ContentValues values,String key,String[] valuesStrings){
		SQLiteDatabase db = this.getWritableDatabase();
		db.update(table_name, values, key+"=?", valuesStrings);
	}
}
